package com.bw.mallshop.mvvm.viewmodel;

import android.os.Bundle;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.databinding.ObservableField;
import androidx.viewpager.widget.ViewPager;

import com.blankj.utilcode.util.LogUtils;
import com.bw.common.entity.BaseEntity;
import com.bw.common.mvvm.viewmodel.BaseViewModel;
import com.bw.mallshop.R;
import com.bw.mallshop.adapter.HomeGoodsAdapter;
import com.bw.mallshop.entity.HomeBannerEntity;
import com.bw.mallshop.entity.HomeGoodsEntity;
import com.bw.mallshop.entity.HomeLoadEntity;
import com.bw.mallshop.entity.HomeMenuEntity;
import com.bw.mallshop.mvvm.model.HomeModel;
import com.bw.mallshop.mvvm.view.HomeDetailActivity;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.scwang.smart.refresh.layout.api.RefreshLayout;
import com.scwang.smart.refresh.layout.listener.OnRefreshLoadMoreListener;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

public class HomeViewModel extends BaseViewModel<HomeModel> implements
        OnRefreshLoadMoreListener, OnItemClickListener , ViewPager.PageTransformer {

    private int page = 1;//当前列表中的page页数
    private int categoryId = 14;//分类id
    public HomeGoodsAdapter adapter = new HomeGoodsAdapter();
    private int[]menuIcons = {R.drawable.ic_zhibo,R.drawable.ic_diannao,
            R.drawable.ic_shouji,R.drawable.ic_pad,R.drawable.ic_jiaju,
            R.drawable.ic_jiadian,R.drawable.ic_yundong,R.drawable.ic_xiuxian};
    private String[] menuNames = {"直播","电脑","手机","Pad",
            "家居","家电","秒杀","休闲"};
    public List<HomeMenuEntity>menues = new ArrayList<>();
    private String[]homeBanneres={"https://ss0.bdstatic.com/70cFvHSh_Q1YnxGkpoWK1HF6hhy/it/u=259364631,2307942273&fm=26&gp=0.jpg",
            "https://ss0.bdstatic.com/70cFuHSh_Q1YnxGkpoWK1HF6hhy/it/u=3765821666,4235344830&fm=26&gp=0.jpg",
            "https://ss0.bdstatic.com/70cFuHSh_Q1YnxGkpoWK1HF6hhy/it/u=3336810809,2729597475&fm=26&gp=0.jpg",
            "https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1503471047&di=679d7a6c499f59d1b0dcd56b62a9aa6c&imgtype=jpg&er=1&src=http%3A%2F%2Fimg.90sheji.com%2Fdianpu_cover%2F11%2F14%2F64%2F55%2F94ibannercsn_1200.jpg"};
    public List<HomeBannerEntity>homeBannerEntities = new ArrayList<>();
    public List<String> txtBanneres = new ArrayList<>();
    public List<String>hotImges = new ArrayList<>();
    public List<HomeBannerEntity>topEntities = new ArrayList<>();
    private final static float MAXSCALLER = 1f;//最大缩放倍数
    private final static float MINSCALLER = 0.7f;//最小缩放倍数

    @Override
    protected void initData() {
        createHeadData();
        page = 1;
        m.requestHomeGoods(createParames()).subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(this);
    }

    //创建首页头布局数据源
    private void createHeadData(){
        //创建头布局menu数据源
        for (int i = 0;i < menuNames.length;i ++){
            String pagePath = "";
            if (i == 6)
                pagePath = "com.bw.mallshop.mvvm.view.SkillListActivity";
            menues.add(new HomeMenuEntity(menuIcons[i],menuNames[i],pagePath));
        }
        //配置头部text数据源
        for (int i = 0;i < menuNames.length;i ++){
            txtBanneres.add(menuNames[i]);
        }
        //配置头部Banner图片数据源
        for (int i = 0;i < homeBanneres.length;i ++){
            homeBannerEntities.add(new HomeBannerEntity(homeBanneres[i],"https://www.baidu.com/"));
        }
        //配置viewpager数据源
        topEntities.add(new HomeBannerEntity("https://ss0.bdstatic.com/70cFvHSh_Q1YnxGkpoWK1HF6hhy/it/u=2069038579,1558413647&fm=26&gp=0.jpg","https://www.baidu.com/"));
        topEntities.add(new HomeBannerEntity("https://ss2.bdstatic.com/70cFvnSh_Q1YnxGkpoWK1HF6hhy/it/u=1975162372,3010774958&fm=26&gp=0.jpg","https://www.baidu.com/"));
        topEntities.add(new HomeBannerEntity("https://ss1.bdstatic.com/70cFuXSh_Q1YnxGkpoWK1HF6hhy/it/u=1034894772,889898861&fm=26&gp=0.jpg","https://www.baidu.com/"));
        topEntities.add(new HomeBannerEntity("http://img.zcool.cn/community/01796c58772f66a801219c77e4fc04.png@1280w_1l_2o_100sh.png","https://www.baidu.com/"));
        topEntities.add(new HomeBannerEntity("http://img.zcool.cn/community/0154805791ffeb0000012e7edba495.jpg@900w_1l_2o_100sh.jpg","https://www.baidu.com/"));
        //添加热门推荐列表数据源
        hotImges.add("https://img14.360buyimg.com/n0/jfs/t3157/231/5756125504/98807/97ab361d/588084a1N06efb01d.jpg");
        hotImges.add("https://img10.360buyimg.com/n7/jfs/t5905/106/1120548052/61075/6eafa3a5/592f8f7bN763e3d30.jpg");
        hotImges.add("https://img10.360buyimg.com/n7/jfs/t5584/99/6135095454/371625/423b9ba5/59681d91N915995a7.jpg");
        hotImges.add("https://img11.360buyimg.com/n7/jfs/t4447/301/1238553109/193354/13c7e995/58db19a7N25101fe4.jpg");
        hotImges.add("https://img14.360buyimg.com/n1/s190x190_jfs/t7525/189/155179632/395056/e200017f/598fb8a4N7800dee6.jpg");
        hotImges.add("https://img10.360buyimg.com/n7/jfs/t5584/99/6135095454/371625/423b9ba5/59681d91N915995a7.jpg");
        hotImges.add("https://img10.360buyimg.com/n7/jfs/t5584/99/6135095454/371625/423b9ba5/59681d91N915995a7.jpg");
    }

    //上拉记载更多方法
    private void loadData(){
        page+=1;
        m.loadHomeGoods(createParames()).subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(this);
    }

    private Map<String,Object>createParames(){
        HashMap<String,Object>map = new HashMap<>();
        map.put("categoryId",categoryId);
        map.put("pageNo",page);
        return map;
    }

    @Override
    protected void result(BaseEntity entity) {
        if(entity instanceof HomeGoodsEntity){
            //刷新数据
            HomeGoodsEntity homeGoodsEntity = (HomeGoodsEntity) entity;
            adapter.setNewInstance(homeGoodsEntity.getData());
        }else if (entity instanceof HomeLoadEntity){
            //加载数据
            HomeLoadEntity homeLoadEntity = (HomeLoadEntity) entity;
            if (homeLoadEntity == null)
                return;
            if (homeLoadEntity.getData() == null)
                return;
            adapter.addData(homeLoadEntity.getData());
        }
    }

    @Override
    public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
        refreshLayout.finishLoadMore();
        loadData();
    }

    @Override
    public void onRefresh(@NonNull RefreshLayout refreshLayout) {
        refreshLayout.finishRefresh();
        initData();
    }

    @Override
    public void onItemClick(@NonNull BaseQuickAdapter<?, ?> adapter,
                            @NonNull View view, int position) {
        HomeGoodsEntity.DataBean bean = this.adapter.getItem(position);
        Bundle bundle = new Bundle();
        bundle.putInt("goodsId",bean.getId());
        startPage(HomeDetailActivity.class,bundle);
    }

    //page 在改变得页
    //position:偏移过度值
    @Override
    public void transformPage(@NonNull View page, float position) {
        if (position<0){
            //当前页向下一页滑动
            float dis = 1+position;
            if (dis<MINSCALLER)
                dis = MINSCALLER;
            page.setScaleY(dis);
        }else if(position >=0 && position<1){

            if (position == 0){
                page.setScaleY(MAXSCALLER);
            }else {
                float dis = (1-position)+MINSCALLER;
                if (dis>MAXSCALLER)
                    dis = MAXSCALLER;
                page.setScaleY(dis);
            }
        }else if (position >=1){
            //设置Y轴缩放->0.7高度
            page.setScaleY(MINSCALLER);
        }

    }
}
